package com.shuda.business.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuda.business.crm.entity.CustomerTransferEntity;
import com.shuda.business.crm.service.CustomerTransferService;
import com.shuda.common.core.domain.R;
import com.shuda.common.core.domain.PageRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 客户转移记录Controller
 * @author shuda
 */
@RestController
@RequestMapping("/crm/customer-transfer")
@Tag(name = "客户转移记录管理", description = "客户转移记录相关接口")
@RequiredArgsConstructor
public class CustomerTransferController {

    private final CustomerTransferService customerTransferService;

    /**
     * 分页查询客户转移记录
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询客户转移记录", description = "根据条件分页查询客户转移记录")
    public R<IPage<CustomerTransferEntity>> getCustomerTransferPage(
            @Parameter(description = "分页请求参数") PageRequest pageRequest,
            @Parameter(description = "客户ID") @RequestParam(required = false) Long customerId,
            @Parameter(description = "转移状态") @RequestParam(required = false) String transferStatus,
            @Parameter(description = "申请人姓名") @RequestParam(required = false) String applicantName) {
        
        Page<CustomerTransferEntity> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        IPage<CustomerTransferEntity> result = customerTransferService.getCustomerTransferPage(page, customerId, transferStatus, applicantName);
        return R.ok(result);
    }

    /**
     * 根据客户ID分页查询转移记录
     */
    @GetMapping("/customer/{customerId}")
    @Operation(summary = "根据客户ID查询转移记录", description = "根据客户ID分页查询转移记录")
    public R<IPage<CustomerTransferEntity>> getCustomerTransferByCustomerId(
            @Parameter(description = "分页请求参数") PageRequest pageRequest,
            @Parameter(description = "客户ID") @PathVariable Long customerId) {
        
        Page<CustomerTransferEntity> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        IPage<CustomerTransferEntity> result = customerTransferService.getCustomerTransferByCustomerId(page, customerId);
        return R.ok(result);
    }

    /**
     * 根据ID获取转移记录
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取转移记录", description = "根据ID获取客户转移记录详情")
    public R<CustomerTransferEntity> getCustomerTransferById(@Parameter(description = "转移记录ID") @PathVariable Long id) {
        CustomerTransferEntity transferEntity = customerTransferService.getCustomerTransferById(id);
        return transferEntity != null ? R.ok(transferEntity) : R.fail("转移记录不存在");
    }

    /**
     * 创建客户转移记录
     */
    @PostMapping
    @Operation(summary = "创建客户转移记录", description = "创建客户转移记录")
    public R<Boolean> createCustomerTransfer(@RequestBody CustomerTransferEntity transferEntity) {
        boolean result = customerTransferService.createCustomerTransfer(transferEntity);
        return result ? R.ok(true) : R.fail("创建转移记录失败");
    }

    /**
     * 更新转移记录
     */
    @PutMapping
    @Operation(summary = "更新转移记录", description = "更新客户转移记录")
    public R<Boolean> updateCustomerTransfer(@RequestBody CustomerTransferEntity transferEntity) {
        boolean result = customerTransferService.updateCustomerTransfer(transferEntity);
        return result ? R.ok(true) : R.fail("更新转移记录失败");
    }

    /**
     * 删除转移记录
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除转移记录", description = "根据ID删除客户转移记录")
    public R<Boolean> deleteCustomerTransfer(@Parameter(description = "转移记录ID") @PathVariable Long id) {
        boolean result = customerTransferService.deleteCustomerTransfer(id);
        return result ? R.ok(true) : R.fail("删除转移记录失败");
    }

    /**
     * 批量删除转移记录
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除转移记录", description = "批量删除客户转移记录")
    public R<Boolean> batchDeleteCustomerTransfer(@RequestBody Long[] ids) {
        boolean result = customerTransferService.batchDeleteCustomerTransfer(ids);
        return result ? R.ok(true) : R.fail("批量删除转移记录失败");
    }

    /**
     * 审批客户转移申请
     */
    @PostMapping("/approve/{transferId}")
    @Operation(summary = "审批客户转移申请", description = "审批客户转移申请")
    public R<Boolean> approveCustomerTransfer(
            @Parameter(description = "转移记录ID") @PathVariable Long transferId,
            @Parameter(description = "审批状态：approved-已批准，rejected-已拒绝") @RequestParam String approveStatus,
            @Parameter(description = "审批备注") @RequestParam(required = false) String approveRemark,
            @Parameter(description = "审批人ID") @RequestParam Long approverId,
            @Parameter(description = "审批人姓名") @RequestParam String approverName) {
        
        boolean result = customerTransferService.approveCustomerTransfer(transferId, approveStatus, approveRemark, approverId, approverName);
        return result ? R.ok(true) : R.fail("审批转移申请失败");
    }

    /**
     * 批量审批客户转移申请
     */
    @PostMapping("/batch-approve")
    @Operation(summary = "批量审批客户转移申请", description = "批量审批客户转移申请")
    public R<Boolean> batchApproveCustomerTransfer(
            @RequestBody Long[] transferIds,
            @Parameter(description = "审批状态：approved-已批准，rejected-已拒绝") @RequestParam String approveStatus,
            @Parameter(description = "审批备注") @RequestParam(required = false) String approveRemark,
            @Parameter(description = "审批人ID") @RequestParam Long approverId,
            @Parameter(description = "审批人姓名") @RequestParam String approverName) {
        
        boolean result = customerTransferService.batchApproveCustomerTransfer(transferIds, approveStatus, approveRemark, approverId, approverName);
        return result ? R.ok(true) : R.fail("批量审批转移申请失败");
    }
}